package com.noname.lib_base_java.util;


import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * Author: Lmoumou
 * Date: 2018-10-15 14:16
 * 文件名: SharedPrefUtil
 * 描述:
 */
public class SharedPrefUtil {
    public static final String NAME = "GRE";

    private static Context context;
    private SharedPreferences prefrence;

    private SharedPrefUtil(String prefrenceName) {
        prefrence = context.getSharedPreferences(prefrenceName, Context.MODE_PRIVATE);
    }

//    public void init(Context context) {
//        SharedPrefUtil.context = context.getApplicationContext();
//    }

    public static void init(Context context) {
        SharedPrefUtil.context = context.getApplicationContext();
    }

    public static SharedPrefUtil open(String name) {
        synchronized (SharedPrefUtil.class) {
            if (SharedPrefUtil.context == null) {
                new RuntimeException("Must call init(Context context) at first");
            }

            return new SharedPrefUtil(name);
        }
    }

    /**
     * 存放String
     */
    public void putString(String key, String value) {
        SharedPreferences.Editor editor = this.prefrence.edit();
        editor.putString(key, value);
        editor.apply();
    }


    /**
     * 获取String
     * <p>
     * 默认返回""
     */
    public String getString(String key) {
        return this.prefrence.getString(key, "");
    }

    /**
     * 存放Int
     */
    public void putInt(String key, int value) {
        SharedPreferences.Editor editor = this.prefrence.edit();
        editor.putInt(key, value);
        editor.apply();
    }


    /**
     * 获取Int
     * <p>
     * 默认返回""
     */
    public int getInt(String key) {
        return this.prefrence.getInt(key, 0);
    }

    /**
     * 获取Int
     * <p>
     * 默认返回""
     */
    public int getInt(String key,int defultValue) {
        return this.prefrence.getInt(key, defultValue);
    }


    /**
     * 存放布尔值
     *
     * @param key
     * @param value
     */
    public void putBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = this.prefrence.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    /**
     * 获取布尔值
     *
     * @param key
     * @return
     */
    public boolean getBoolean(String key) {
        return this.prefrence.getBoolean(key, false);
    }


    /**
     * 存放对象
     */
    public void putEntity(String key, Object value) {
        if (value instanceof Serializable) {
            try {
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(bs);
                oos.writeObject(value);
                oos.flush();
                oos.close();
                byte[] data = bs.toByteArray();
                String base64Str = Base64.encodeToString(data, 2);
                this.putString(key, base64Str);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取对象
     */
    public Object getEntity(String key) {
        try {
            String s = this.getString(key);
            if (TextUtils.isEmpty(s)) {
                return null;
            }
            byte[] data = Base64.decode(s, 2);
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            ObjectInputStream ois = new ObjectInputStream(bis);
            Object o = ois.readObject();
            ois.close();
            return o;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 移除对象
     */
    public void remove(String key) {
        SharedPreferences.Editor editor = this.prefrence.edit();
        editor.remove(key);
        editor.apply();
    }
}
